package main.FileVisitors.FileVisitorImpl;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import main.FileVisitors.MyFileVisitor;

public class AtomicFileVisitor extends MyFileVisitor {

    private AtomicInteger files = new AtomicInteger(0);
    private AtomicInteger folders = new AtomicInteger(0);
    private AtomicLong totalSize = new AtomicLong(0);

    public AtomicFileVisitor() {
    }

    public AtomicFileVisitor(String lookFor) {
        this.lookFor = lookFor;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        folders.incrementAndGet();
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        if (file.toString().contains(lookFor)) {
            matches.add(file);
        }
        files.incrementAndGet();
        totalSize.addAndGet((new File(file.toString())).length());
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
        // System.out.printf("Failed on: %s.\n", file.getFileName());
        return FileVisitResult.CONTINUE;
    }

    /**
     * @return the files
     */
    public int getFiles() {
        return files.intValue();
    }

    /**
     * @return the folders
     */
    public int getFolders() {
        return folders.intValue();
    }

    /**
     * @return the totalSize
     */
    public long getTotalSize() {
        return totalSize.longValue();
    }
}
